package com.jz.dao.impl;

import com.jz.dao.BaseDao;
import com.jz.dao.UserDao;
import com.jz.entity.User;

import java.util.Date;
import java.util.List;

public class UserDaoImpl extends BaseDao<User> implements UserDao {

    @Override
    public User login(String username, String password) {
        String sql="select * from user where ( username = ? or phone= ?) and password = ? and status = 1";
        return querySingle(sql,User.class,username,username,password);
    }

    @Override
    public List<User> queryUser(String name, int pageSize, int curPage) {
        StringBuffer sql=new StringBuffer("select * from user where 1=1 ");
        if(name!=null&&!"".equals(name.trim())){
            sql.append(" and name like '%"+name+"%' ");
        }
        sql.append("limit ?,?");
        return queryMore(sql.toString(),User.class,(curPage-1)*pageSize,pageSize);
    }

    @Override
    public int queryUserCount(String name) {
        StringBuffer sql=new StringBuffer("select count(*) from user where 1=1 ");
        if(name!=null&&!"".equals(name.trim())){
            sql.append(" and name like '%"+name+"%' ");
        }
        return queryScar(sql.toString());
    }

    @Override
    public User queryUserById(String uid) {
        String sql = "select * from user where uid = ?";
        return querySingle(sql,User.class,uid);
    }
 //添加
    @Override
    public int addUser(User u) {
        String sql="insert into user(name,phone,age,sex,username,password,status,createtime,role,picture)values(?,?,?,?,?,?,?,?,?,?)";
        Object[] params={u.getName(),u.getPhone(),u.getAge(),u.getSex(),u.getUsername(),u.getPassword(),u.getStatus(),new Date(),u.getRole(),u.getPicture()};
        return update(sql,params);
    }
    //根据多选框里面的uid删除
    @Override
    public int deleteUser(String uid) {
        //uid应为一个字符串用逗号隔开
        String sql ="delete from user where uid in ("+uid+")";
        return update(sql);

    }

    @Override
    //修改
    public int updateUser(User u) {
        String sql="update user set name=?,phone=?,age=?,sex=?,username=?,password=?,status=?,role=?,picture=? where uid =?";
        Object[] params={u.getName(),u.getPhone(),u.getAge(),u.getSex(),u.getUsername(),u.getPassword(),u.getStatus(),u.getRole(),u.getPicture(),u.getUid()};
        return update(sql,params);
    }
}
